package com.nearby.android;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
//指南针
public class Compass extends Activity implements SensorEventListener{
	ImageView znzImage;
	float currentDegree = 0f;
	
	SensorManager mSensorManager;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compass);
		znzImage = (ImageView)findViewById(R.id.znzImage);
		mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onResume(){
		super.onResume();
		mSensorManager.registerListener(this,
			mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
			SensorManager.SENSOR_DELAY_GAME);
	}
	
	@Override
	protected void onPause(){
		mSensorManager.unregisterListener(this);
		super.onPause();
	}
	
	protected void onStop(){
		mSensorManager.unregisterListener(this);
		super.onStop();
	}
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		int sensorType = event.sensor.getType();
		switch(sensorType){
			case Sensor.TYPE_ORIENTATION:
				float degree = event.values[0];
				RotateAnimation ra = new RotateAnimation(currentDegree, -degree, 
						Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
				ra.setDuration(200);
				znzImage.startAnimation(ra);
				currentDegree = -degree;
			break;
		}
		
	}

}
